package com.intelligentpension.kotlinbluetooth.bt

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import com.intelligentpension.kotlinbluetooth.utils.ThreadUtils

/**
 * 版权：点赞 版权所有
 * author :赵航
 * @e-mail: zhaohangcn@gmail.com
 * time：2022/3/29
 * describe ：
 */
class BtClient internal constructor(mListener: Listener?) : BtBase(mListener) {
    @SuppressLint("MissingPermission")
    fun connect(dev: BluetoothDevice) {
        closeSocket()
        try {
            var safeSocket: BluetoothSocket =
                dev.createRfcommSocketToServiceRecord(SPP_UUID)//加密传输,Android系统强制配对,弹窗显示配对码
            var unsafeSocket: BluetoothSocket =
                dev.createInsecureRfcommSocketToServiceRecord(SPP_UUID)//铭文传输(不安全)
            ThreadUtils.executeSubThread {
                loopRead(unsafeSocket)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            closeSocket()
        }

    }
}